package com.interauto.service;

import com.interauto.Dao.Mapper.MgCaseRuntInter;
import com.interauto.Dao.Mapper.MgInteruntInter;
import com.interauto.Dao.entity.Caserunt;
import com.interauto.Dao.entity.Interunt;
import com.interauto.Dao.entity.Runtasktable;
import com.interauto.bean.AutoAssInfoBean;
import com.interauto.bean.AutoCaseBean;
import com.interauto.bean.AutoInterCaseBean;
import com.interauto.bean.httpinfo.HttpRequestBean;
import com.interauto.bean.httpinfo.HttpResPonseBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.interauto.util.AutoStringUtils.getJsontoMap;

/**
 * 处理运行结果保存相关
 */
@Component
public class AutoRunResultService {
    private static final Logger logger= LoggerFactory.getLogger(AutoRunCaseinfoService.class);
    @Autowired
    private MgCaseRuntInter mgCaseRuntInter;
    @Autowired
    private MgInteruntInter mgInteruntInter;

    /**
     * 保存用例的运行信息
     * @param autoCaseBean 运行的用例
     * @return 运行结果的用例表的主键ID
     */
    public int addcaserunt(AutoCaseBean autoCaseBean,Runtasktable runtasktable){
        if(autoCaseBean == null){
            return -1;
        }
        try{
            Caserunt caserunt=new Caserunt();
            caserunt.setTaskid(runtasktable.getId());
            caserunt.setTaskname(runtasktable.getTaskname());
            caserunt.setStatus(0);
            caserunt.setCasename(autoCaseBean.getCasename());
            caserunt.setCaseid(autoCaseBean.getCaseid());
            mgCaseRuntInter.addcaserunt(caserunt);
            logger.info("--->>>>addcaserunt is:"+caserunt.toString());
            return caserunt.getId();
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error("-->addcaserunt is system error-->+"+e);
        }
        return -1;
    }

    /**
     * 修改用例的运行状态
     * @param caseruntid 要修改的运行的用例信息
     * @param status 1=运行成功,2=运行失败
     */
    public void udcaserunt(int caseruntid,int status){
        logger.info(String.format("udcaserunt-->caseruntid:%s,status:%s",caseruntid,status));
        try{
            mgCaseRuntInter.upstatuscaserunt(caseruntid,status,new Date());
        }
        catch (Exception e){
            logger.error("upstatuscaserunt is system error---->"+e);
        }
    }


    /**
     * 往接口运行结果表里存在运行信息
     * @param autointerinfo 运行的接口信息
     * @param httpResPonseBean 运行结果的响应信息，前期就http
     * @param caseruntid 所关联的运行的用例ID
     * @param message 接口跑完，对应的跑的结果信息
     * @param runstatus 接口运行成功还是失败; 1=运行成功，2=运行失败
     */
    public int addinterrunt(AutoInterCaseBean autointerinfo, HttpResPonseBean httpResPonseBean,
                            String message, int caseruntid, int runstatus){
        if(StringUtils.isNoneEmpty(message) && message.length()>8000){  //太长了，数据库存不下了
            message=message.substring(0,8000);
        }
        Interunt interunt=getInterunt(autointerinfo,httpResPonseBean,message,caseruntid,runstatus);
        logger.info("addinterrunt is interunt info--->"+interunt.toString());
        try{
            String resinfo=interunt.getResinfo();
            if(StringUtils.isNoneEmpty(resinfo) && resinfo.length()>8000){
                interunt.setResinfo(resinfo.substring(0,8000));
            }
            mgInteruntInter.addinterrunt(interunt);
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error("--->>>addinterrunt is system error-->"+e);
        }
        return interunt.getId();
    }

    public Interunt getInterunt(AutoInterCaseBean autointerinfo, HttpResPonseBean httpResPonseBean,
                                String message, int caseruntid, int runstatus){
        Interunt interunt=new Interunt();
        interunt.setStatus(runstatus);
        interunt.setCaseruntid(caseruntid);
        interunt.setInterfacename(autointerinfo.getInterfacename());
        interunt.setIntercasetype(autointerinfo.getIntercasetype());
        interunt.setMessage(message);
        if(httpResPonseBean!=null){
            interunt.setRestype(httpResPonseBean.getRestype());
            interunt.setRescode(httpResPonseBean.getRescode());
            interunt.setResinfo(httpResPonseBean.getResinfo());
            interunt.setResheader(getJsontoMap(httpResPonseBean.getResheader()));
            interunt.setRescookie(getJsontoMap(httpResPonseBean.getRescookie()));
        }
        List<AutoAssInfoBean> autoAssInfoBeans=autointerinfo.getAutoAssInfoBeans();
        if(autoAssInfoBeans!=null && autoAssInfoBeans.size()>0){
            interunt.setAutoassinfo(getJsontoMap(autoAssInfoBeans));  //存入断言信息
        }
        HttpRequestBean httpRequestBean=autointerinfo.getHttpRequestBean();
        if(httpRequestBean!=null){
            interunt.setHttpmeth(httpRequestBean.getHttpmeth().getHttpmethtype());
            interunt.setHttpurl(httpRequestBean.getHttpurl());
            interunt.setHttpparam(httpRequestBean.getHttpparam());
            Map<String, String> cookiemap=httpRequestBean.getHttpcookie();
            Map<String, String> headermap=httpRequestBean.getHttpheader();
            if(cookiemap!=null && cookiemap.size()>0){
                interunt.setHttpcookie(getJsontoMap(cookiemap));
            }
            if(headermap!=null && headermap.size()>0){
                interunt.setHttpheader(getJsontoMap(headermap));
            }
            interunt.setPostparamtype(httpRequestBean.getPostparamtype());
        }
        return interunt;
    }
}
